home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / COSEmulator / COSEmulator- SRC / src / OffScreen.cpp < prev    next >
Encoding:
Text File  |  1998-06-21  |  5.9 KB  |  240 lines  |  [TEXT/CWIE]

  1. //===================================================================
  2. //=======================  Headers        =============================
  3.  
  4. #include "Blitters.h"
  5. #include "GameUtilities.h"
  6.  
  7. #include <string.h>
  8. //===================================================================
  9. //=======================  Globals        =============================
  10.  
  11.  
  12. //===================================================================
  13. //=======================  #define        =============================
  14.  
  15.  
  16. //===================================================================
  17. //=======================  Function Prototypes    =====================
  18.  
  19. /*----------------------------------------------------------------------------\
  20.  
  21.     OffScreenBuff :: Constructor
  22.  
  23. \----------------------------------------------------------------------------*/
  24.  
  25.     OffScreenBuff :: OffScreenBuff( void )
  26. {
  27.     declared = false;
  28.     memset( &theOffScreen , 0 , sizeof( theOffScreen ) );
  29. }
  30.  
  31. /*----------------------------------------------------------------------------\
  32.  
  33.     OffScreenBuff :: Destructor
  34.  
  35. \----------------------------------------------------------------------------*/
  36.  
  37.     OffScreenBuff :: ~OffScreenBuff( void )
  38. {
  39.     DeleteBuff();
  40. }
  41.  
  42. /*----------------------------------------------------------------------------\
  43.  
  44.     OffScreenBuff :: LoadPicBuff
  45.  
  46. - load the off screen from a picture
  47.  
  48. \----------------------------------------------------------------------------*/
  49.  
  50. Boolean    OffScreenBuff :: LoadPicBuff( ushort picID )
  51. {
  52.     PicHandle spritePict;        // the sprite picture
  53.     CGrafPtr oldCPort;            // the graf port that is in place when we are called
  54.     GDHandle oldDevice;            // the gdevice that is in place when we are called
  55.     OSErr    error;
  56.     
  57.     
  58.     // load the pict
  59.     spritePict = GetPicture( picID );
  60.     if ( spritePict == ( PicHandle )nil )
  61.     {
  62.     //    Warning("cant load picture" , picID );
  63.         DebugStr( "\p LoadOffScreen " );
  64.         Debugger();
  65.         return false;
  66.     }
  67.     
  68.     // copy its bounds rect
  69.     theOffScreen.Bounds = RTor( ( *spritePict )->picFrame );
  70.     
  71.     
  72.     /*************************************************************************/
  73.     /* Allocate a new GWorld for the offscreen drawing and store its PixMap. */
  74.     /*************************************************************************/
  75.  
  76.     error = NewGWorld( &(theOffScreen.GWorld), 16, &rToR( theOffScreen.Bounds ), 
  77.             NULL, ( GDHandle )NULL, keepLocal );//keepLocal
  78.  
  79.     
  80.     if( error != noErr )
  81.         return false;
  82.         
  83.     
  84.     
  85.     declared = true;
  86.     
  87.     // extract the pixmap handle
  88.     theOffScreen.PixMap = GetGWorldPixMap( theOffScreen.GWorld );
  89.     
  90.     LockPixels( theOffScreen.PixMap );
  91.     // save the current port and gdevice
  92.     GetGWorld( &oldCPort, &oldDevice );
  93.     
  94.     // set the offscreen buffer as current ( and lock the pixel map )
  95.     SetGWorld( theOffScreen.GWorld, ( GDHandle )nil );
  96.     
  97.     // draw the picture
  98.     EraseRect( &( rToR( theOffScreen.Bounds ) ) );
  99.     DrawPicture( spritePict, &( rToR( theOffScreen.Bounds ) ) );
  100.     
  101.     // restore the current port and gdevice
  102.     //UnlockPixels( theOffScreen.PixMap ); rest of program assume it is locked
  103.     SetGWorld( oldCPort, oldDevice );
  104.  
  105.     // dump the pict
  106.     ReleaseResource( ( Handle )spritePict );
  107.     
  108.     return true;
  109.     
  110. }
  111.  
  112. /*----------------------------------------------------------------------------\
  113.  
  114.     OffScreenBuff :: NewBuff
  115.  
  116. -create an empty off screen of this size
  117.  
  118. \----------------------------------------------------------------------------*/
  119.  
  120. Boolean    OffScreenBuff :: NewBuff( const rect &size )
  121. {
  122. OSErr        error;
  123.  
  124. theOffScreen.Bounds = size;
  125.  
  126. error = NewGWorld(    &(theOffScreen.GWorld), 16, &(rToR( theOffScreen.Bounds )),
  127.                      NULL, ( GDHandle )nil, keepLocal ); //keepLocal
  128.  
  129.  
  130.  
  131. theOffScreen.PixMap = GetGWorldPixMap( theOffScreen.GWorld );
  132.  
  133. LockPixels( theOffScreen.PixMap );
  134.  
  135. if( error != noErr)
  136.     return false;
  137.     
  138. declared = true;
  139.  
  140. return true;
  141.     
  142. }
  143.  
  144. /*----------------------------------------------------------------------------\
  145.  
  146.     OffScreenBuff :: NewBuff
  147.  
  148. -create an empty off screen of this size
  149.  
  150. \----------------------------------------------------------------------------*/
  151.  
  152. Boolean    OffScreenBuff :: NewBuff( ushort width , ushort height )
  153. {
  154. OSErr        error;
  155.  
  156. theOffScreen.Bounds.left = 0;
  157. theOffScreen.Bounds.top = 0;
  158. theOffScreen.Bounds.right = width;
  159. theOffScreen.Bounds.bottom = height;
  160.  
  161. error = NewGWorld(    &(theOffScreen.GWorld), 16, &(rToR( theOffScreen.Bounds )),
  162.                      NULL, ( GDHandle )nil, keepLocal ); //keepLocal
  163.  
  164.  
  165.  
  166. theOffScreen.PixMap = GetGWorldPixMap( theOffScreen.GWorld );
  167.  
  168. LockPixels( theOffScreen.PixMap );
  169.  
  170. if( error != noErr)
  171.     return false;
  172.     
  173. declared = true;
  174.  
  175. return true;
  176.     
  177. }
  178.  
  179. /*----------------------------------------------------------------------------\
  180.  
  181.     OffScreenBuff :: GetSrcPtr
  182.  
  183. \----------------------------------------------------------------------------*/
  184.  
  185. ushort    *OffScreenBuff :: GetSrcPtr( void )
  186. {
  187.     return( (ushort *)(GetPixBaseAddr( theOffScreen.PixMap ) ));
  188. }
  189.  
  190. /*----------------------------------------------------------------------------\
  191.  
  192.     OffScreenBuff :: GetRowSize
  193.  
  194. \----------------------------------------------------------------------------*/
  195.  
  196. ulong    OffScreenBuff :: GetRowSize( void )
  197. {
  198.     return( ( *(theOffScreen.PixMap) )->rowBytes & 0x3FFF );
  199. }
  200.  
  201. /*----------------------------------------------------------------------------\
  202.  
  203.     OffScreenBuff :: GetOffScreen
  204.  
  205. \----------------------------------------------------------------------------*/
  206.  
  207. OffScreen    *OffScreenBuff :: GetOffScreen( void )
  208. {
  209.     return( &theOffScreen );
  210. }
  211.  
  212. /*----------------------------------------------------------------------------\
  213.  
  214.     OffScreenBuff :: GetBoundsSize
  215.  
  216. \----------------------------------------------------------------------------*/
  217.  
  218. rect    OffScreenBuff :: GetBoundsSize( void )
  219. {
  220.     return( theOffScreen.Bounds );
  221. }
  222.  
  223. /*----------------------------------------------------------------------------\
  224.  
  225.     OffScreenBuff :: DeleteBuff
  226.  
  227. -checks to see if it has been declared before trying to delete it
  228.  
  229. \----------------------------------------------------------------------------*/
  230.  
  231. void    OffScreenBuff :: DeleteBuff( void )
  232. {
  233.     if( declared )
  234.     {
  235.         DisposeGWorld( theOffScreen.GWorld );
  236.         declared = false;
  237.     }
  238. }
  239.  
  240.